<template>
  <el-card shadow="always" :body-style="{ padding: '20px' }">
    <el-form :inline="true" class="demo-form-inline" :disabled="disabled">
      <el-form-item label="一级分类">
        <el-select
          placeholder="请选择"
          v-model="(category1Id as number)"
          @change="category1Change"
        >
          <el-option
            v-for="item in categoryList1"
            :label="item.name"
            :value="item.id"
            :key="item.id"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="二级分类">
        <el-select
          placeholder="请选择"
          v-model="(category2Id as number)"
          @change="category2Change"
        >
          <el-option
            v-for="item in categoryList2"
            :label="item.name"
            :value="item.id"
            :key="item.id"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="三级分类">
        <el-select
          placeholder="请选择"
          v-model="(category3Id as number)"
          
        >
          <el-option
            v-for="item in categoryList3"
            :label="item.name"
            :value="item.id"
            :key="item.id"
          />
        </el-select>
      </el-form-item>
    </el-form>
  </el-card>
</template>

<script lang="ts">
import { defineComponent } from "vue";
export default defineComponent({
  name: "Category",
});
</script>
<script setup lang="ts">
import type { reqCategoryItemType, reqCategoryType } from "@/api/attr";
import { onMounted } from "vue";
import { storeToRefs } from "pinia";
// 引入attr仓库实例对象
import useAttrStore from "@/stores/attr";
defineProps<{
  disabled:boolean
}>()
//获取attr仓库实例对象
const attrStore = useAttrStore();
//接受attr仓库中的state数据
const {
  categoryList1,
  category1Id,
  categoryList2,
  category2Id,
  categoryList3,
  category3Id,
} = storeToRefs(attrStore);

onMounted(() => {
  //调用store中的方法,发送1级分类请求
  attrStore.getCategory1();
});

//一级分类被选择后,处理二级分类请求
const category1Change = () => {
    categoryList2.value=[];
    category2Id.value=null;
    categoryList3.value=[];
    category3Id.value=null;
  //调用store中的方法,发送2级分类请求
  attrStore.getCategory2();
};

//二级分类被选择后,处理三级分类请求
const category2Change = () => {
    categoryList3.value=[];
    category3Id.value=null;
  //调用store中的方法,发送3级分类请求
  attrStore.getCategory3();
};
</script>
<style scoped></style>
